{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Shape Manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# change the shape of an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.floor(10*np.random.random((3,4)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[3., 9., 7., 2.],\n       [5., 8., 1., 8.],\n       [6., 7., 5., 3.]])"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(3, 4)"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**以下三个命令可以改变数组的形状，但是不会改变原来的数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([3., 9., 7., 2., 5., 8., 1., 8., 6., 7., 5., 3.])"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "a.ravel()   # 返回的是扁平化的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[3., 9.],\n       [7., 2.],\n       [5., 8.],\n       [1., 8.],\n       [6., 7.],\n       [5., 3.]])"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "a.reshape(6, 2) # 返回修改形状的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[3., 5., 6.],\n       [9., 8., 7.],\n       [7., 1., 5.],\n       [2., 8., 3.]])"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "a.T # 返回转置后的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(4, 3)"
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "a.T.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(3, 4)"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "a.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**`resize`在原数组基础上进行修改**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[3., 9., 7., 2.],\n       [5., 8., 1., 8.],\n       [6., 7., 5., 3.]])"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.resize((2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[3., 9., 7., 2., 5., 8.],\n       [1., 8., 6., 7., 5., 3.]])"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stacking together different arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.floor(10*np.random.random((2, 2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5., 9.],\n       [0., 4.]])"
     },
     "metadata": {},
     "execution_count": 16
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.floor(10*np.random.random((2, 2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[6., 1.],\n       [7., 4.]])"
     },
     "metadata": {},
     "execution_count": 18
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5., 9.],\n       [0., 4.],\n       [6., 1.],\n       [7., 4.]])"
     },
     "metadata": {},
     "execution_count": 19
    }
   ],
   "source": [
    "np.vstack((a, b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5., 9., 6., 1.],\n       [0., 4., 7., 4.]])"
     },
     "metadata": {},
     "execution_count": 20
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[5., 9., 6., 1.],\n       [0., 4., 7., 4.]])"
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "np.column_stack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([4., 2.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([3., 8.])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[4., 3.],\n       [2., 8.]])"
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "np.column_stack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([4., 2., 3., 8.])"
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[4.],\n       [2.]])"
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "a[:,newaxis]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[4., 3.],\n       [2., 8.]])"
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "np.column_stack((a[:newaxis], b[:, newaxis]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[4., 3.],\n       [2., 8.]])"
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "np.hstack((a[:, newaxis], b[:, newaxis]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([1, 2, 3, 0, 4])"
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "np.r_[1:4,0,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Splitting one array into several smaller ones"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.floor(10*np.random.random((2,12)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "array([[2., 8., 9., 7., 1., 8., 7., 0., 0., 9., 3., 7.],\n       [0., 2., 1., 1., 3., 0., 7., 4., 1., 4., 9., 6.]])"
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[array([[2., 8., 9., 7.],\n        [0., 2., 1., 1.]]),\n array([[1., 8., 7., 0.],\n        [3., 0., 7., 4.]]),\n array([[0., 9., 3., 7.],\n        [1., 4., 9., 6.]])]"
     },
     "metadata": {},
     "execution_count": 32
    }
   ],
   "source": [
    "np.hsplit(a,3) # 分为三个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "[array([[2., 8., 9.],\n        [0., 2., 1.]]),\n array([[7.],\n        [1.]]),\n array([[1., 8., 7., 0., 0., 9., 3., 7.],\n        [3., 0., 7., 4., 1., 4., 9., 6.]])]"
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "np.hsplit(a,(3,4)) # 在第三个和第四个列分割"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python36964bit993ec77479a3441f817fdc4955f087a8",
   "display_name": "Python 3.6.9 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}